<template>
    <div>
        <div>
            <el-input
                    size="small"
                    class="addPosInput"
                    placeholder="添加职位"
                    suffix-icon="el-icon-circle-plus"
                    v-model="pos.name"
                    @keydown.enter.native="addPosition">
            </el-input>
            <el-button type="warning" icon="el-icon-search" size="small" @click="addPosition" round>添加</el-button>
            <el-button type="danger" icon="el-icon-delete" size="small" :disabled="this.multipleSelection.length==0"
                       round @click="deleteAll">批量删除
            </el-button>
        </div>
        <div class="posTable">
            <el-table
                    ref="multipleTable"
                    stripe
                    border
                    fit="true"
                    size="medium"
                    :data="position"
                    tooltip-effect="dark"
                    style="width: 105%"
                    @selection-change="handleSelectionChange">
                <el-table-column
                        type="selection"
                        width="60">
                </el-table-column>
                <el-table-column
                        prop="id"
                        label="编号"
                        width="60">
                </el-table-column>
                <el-table-column
                        prop="name"
                        label="职位"
                        width="220">
                </el-table-column>
                <el-table-column
                        prop="createDate"
                        sortable
                        label="创建时间"
                        width="220">
                </el-table-column>
                <el-table-column
                        prop="enabled"
                        label="状态"
                        width="120"
                        show-overflow-tooltip>
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
                        <el-tag v-else type="danger">未启用</el-tag>
                    </template>
                </el-table-column>
                <el-table-column
                        label="操作">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="primary"
                                icon="el-icon-edit"
                                round
                                @click="showEditView(scope.$index, scope.row)">编辑
                        </el-button>
                        <el-button
                                size="mini"
                                type="danger"
                                icon="el-icon-delete"
                                round
                                @click="handleDelete(scope.$index, scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-dialog title="编辑职位" :visible.sync="dialogVisible" width="20%">
            <div>
                <el-tag>职位名称</el-tag>
                <el-input size="small" v-model="updatePos.name" class="updatePosInput"
                          suffix-icon="el-icon-suitcase"></el-input>
                <el-switch style="margin-top: 25px;margin-left: 15px"
                           v-model="updatePos.enabled"
                           active-color="#13ce66"
                           inactive-color="#ff4949"
                           active-text="启用"
                           inactive-text="不启用">
                </el-switch>
            </div>
            <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false" round>取 消</el-button>
        <el-button size="small" type="primary" @click="toUpdatePos" round>确 定</el-button>
      </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "PosMana",
        data() {
            return {
                pos: {
                    name: ''
                },
                position: [],
                dialogVisible: false,
                updatePos: {
                    id: '',
                    name: '',
                    enabled: ''
                },
                multipleSelection: []
            }
        },
        mounted() {
            this.initPositions();
        },
        methods: {
            deleteAll(){
                this.$confirm('此操作将永久删除[' + this.multipleSelection.length + ']个职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let ids='?';
                    this.multipleSelection.forEach(item=>{
                        ids+='ids='+item.id+'&';
                    })
                    this.deleteRequest('/system/basic/position/' + ids).then(resp => {
                        if (resp) {
                            this.initPositions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            toUpdatePos() {
                if (this.updatePos.name) {
                    this.updatePos.createDate = '';
                    this.putRequest('/system/basic/position/', this.updatePos).then(resp => {
                        if (resp) {
                            this.initPositions();
                            this.dialogVisible = false;
                        }
                    })
                } else {
                    this.$message.error('职位名称不能为空！')
                }
            },
            addPosition() {
                if (this.pos.name) {
                    this.postRequest('/system/basic/position/', this.pos).then(resp => {
                        if (resp) {
                            this.initPositions();
                            this.pos.name = '';
                        }
                    })
                } else {
                    this.$message.error('请输入职位');
                }
            },
            showEditView(index, data) {
                Object.assign(this.updatePos, data);
                this.updatePos.createDate = '';
                this.dialogVisible = true;
            },
            handleDelete(index, data) {
                this.$confirm('此操作将永久删除[' + data.name + ']职位, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/system/basic/position/' + data.id).then(resp => {
                        if (resp) {
                            this.initPositions();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            initPositions() {
                this.getRequest('/system/basic/position/').then(resp => {
                    if (resp) {
                        this.position = resp;
                    }
                })
            }
        },
        filterHandler(value, row, column) {
            const property = column['property'];
            return row[property] === value;
        }
    }
</script>

<style scoped>
    .addPosInput {
        width: 320px;
        margin-right: 15px;
    }

    .posTable {
        margin-top: 15px;
    }

    .updatePosInput {
        width: 200px;
        margin-left: 15px;
    }
</style>